package explorandum.g1;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import explorandum.Logger;
import explorandum.Move;
import explorandum.Player;

public class G1Trapstar implements Player
{
	Logger log;
	Random rand;

	public void register(int explorerID, int rounds, int explorers, int range, Logger _log, Random _rand)
	{
		log = _log;
		rand = _rand;
	}

	public Color color() throws Exception
	{
		return Color.RED;
	}
	
	//public Move follow(String type, Point currentLocation, Point[] offsets, Boolean[] hasExplorer, Integer[][] otherExplorers, Integer[] terrain, int time){
		
	//}

	public Move move(Point currentLocation, Point[] offsets, Boolean[] hasExplorer, Integer[][] otherExplorers, Integer[] terrain, int time) throws Exception
	{

		String n = "false";
		String e = "false";
		String s = "false";
		String w = "false";
		String se = "false";
		String ne = "false";
		String sw = "false";
		String nw = "false";
		
		
		//To build a list of adjacent water and land cells
		ArrayList<Integer> adjWater = new ArrayList<Integer>();
		ArrayList<Integer> adjLand = new ArrayList<Integer>();

		//Loop through adjacent cells
		for (int i = 0; i < _dx.length; i++)
			if(_dx[i] != 0 || _dy[i] != 0)
			{
				//Find this cell in visible cells list
				for(int j = 0; j < offsets.length; j++)
				{
					Point point = offsets[j];
					
					//if adjacent cell and WATER
					if(point.x == currentLocation.x + _dx[i] && point.y == currentLocation.y + _dy[i] && terrain[j] == WATER)
					{
						//System.out.println("AdjWater found!");
						//directions water can be found in
						//cardinal directions
						if(_dx[i] == 1 && _dy[i] == 0)
							e = "true";
						if(_dx[i] == -1 && _dy[i] == 0)
							w = "true";
						if(_dy[i] == -1 && _dx[i] == 0)
							n = "true";
						if(_dy[i] == 1 && _dx[i] == 0)
							s = "true";
						
						//non-cardinal directions
						if(_dx[i] == 1 && _dy[i] == 1)
							se = "true";
						if(_dx[i] == -1 && _dy[i] == 1)
							sw = "true";
						if(_dy[i] == -1 && _dx[i] == 1)
							ne = "true";
						if(_dy[i] == -1 && _dx[i] == -1)
							nw = "true";
						//add to valid actions list
						//(i is the index of the action needed to move to this cell)
						adjWater.add(i);
						break;
					}
					
					//if adjacent and LAND
					if(point.x == currentLocation.x + _dx[i] && point.y == currentLocation.y + _dy[i] && terrain[j] == LAND){
						adjLand.add(i);
						break;
					}
					
					if(point.x == currentLocation.x + _dx[i] && point.y == currentLocation.y + _dy[i] && terrain[j] == MOUNTAIN)
					{
						//System.out.println("AdjWater found!");
						//directions water can be found in
						//cardinal directions
						if(_dx[i] == 1 && _dy[i] == 0)
							e = "mtn";
						if(_dx[i] == -1 && _dy[i] == 0)
							w = "mtn";
						if(_dy[i] == -1 && _dx[i] == 0)
							n = "mtn";
						if(_dy[i] == 1 && _dx[i] == 0)
							s = "mtn";
						
						//non-cardinal directions
						if(_dx[i] == 1 && _dy[i] == 1)
							se = "mtn";
						if(_dx[i] == -1 && _dy[i] == 1)
							sw = "mtn";
						if(_dy[i] == -1 && _dx[i] == 1)
							ne = "mtn";
						if(_dy[i] == -1 && _dx[i] == -1)
							nw = "mtn";
						//add to valid actions list
						//(i is the index of the action needed to move to this cell)
						break;
					}
				}
			}
		
		//if no adjacent water, move randomly
		//will change to move toward water if visible
		if(adjWater.size()==0){
			return new Move(adjLand.get(rand.nextInt(adjLand.size())));
		}
		
		//for debugging purposes
		//System.out.println("e: " + e + " w: " + w + " s: " + s + " n: " + n);
		//System.out.println("se: " + se + " sw: " + sw + " ne: " + ne + " nw: " + nw);
		//System.out.println("------");
		
		
		//move based on where water is
		if(n == "true" && e != "true"){
			if(e != "mtn")
				return new Move(EAST);
		}
		if(e == "true" && s != "true"){
			if(s != "mtn")
				return new Move(SOUTH);
		}
		if(s == "true" && w != "true"){
			if(w != "mtn")
				return new Move(WEST);
		}
		if(w == "true" && n != "true"){
			if(n != "mtn")
				return new Move(NORTH);
		}
		
		if(ne == "true"){
			if(se != "mtn")
				return new Move(SOUTHEAST);
		}
		if(nw == "true"){
			if(ne != "mtn")
				return new Move(NORTHEAST);
		}
		if(se == "true"){
			if(sw != "mtn")
				return new Move(SOUTHWEST);
		}
		if(sw == "true"){
			if(nw != "mtn")
				return new Move(NORTHWEST);
		}
		

		//pick a random value from valid actions
		return new Move(adjWater.get(rand.nextInt(adjLand.size())));
	}
	
	public boolean isValid(String in, String [][] vals){
		for(int i = 0; i < vals.length;i++){
			if(vals[i][0] == in){
				if(vals[i][1] == "false")
					return true;
			}
		}
		return false;
	}

	public String name() throws Exception
	{
		return "G1 Trapstar";
	}
}